//
//  XDHomeButtonItemCell.m
//  XDEducation
//
//  Created by apple on 2017/4/13.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDHomeButtonItemCell.h"
#import "XDImageButton.h"
@interface XDHomeButtonItemCell ()
{
    NSMutableArray *_dataArr;
}
@end

//static NSString *const cellId = @"XDHomeButtonItemCell";

@implementation XDHomeButtonItemCell

+(instancetype)cellWithTableView:(UITableView *)tableView dataArr:(NSMutableArray *)dataArr cellId:(NSString *)cellId{

    XDHomeButtonItemCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (!cell) {
        cell = [[XDHomeButtonItemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId dataArr:dataArr];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier dataArr:(NSMutableArray <NSDictionary *>*)dataArr{

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        _dataArr = dataArr;
        
        CGFloat itemW = 50 * ScaleWidth;
        CGFloat itemH = 100 * ScaleHeight;
       
        
        CGFloat padding = (ScreenW - 4 * itemW)/5;
        
        
        if (dataArr.count == 3) {
            padding = (ScreenW - 3 * itemW)/4;
        }
        
        CGFloat itemX = padding;
        CGFloat itemY = 10;
        
        int tempI = 0;
        int tempJ = 0;
        for (int i = 0; i < dataArr.count; i ++) {
            
            NSString *itemName = dataArr[i][@"itemName"];
            NSString *itemIcon = dataArr[i][@"itemIcon"];
            
            
            BOOL isMultipleOfFour = !(i % 4);
            
            if (isMultipleOfFour && i != 0) {
                tempJ ++;
                tempI = 0;
                itemY = 10 + (itemH + 10) * tempJ;
            }
            
            
            XDImageButton *button = [XDImageButton  buttonWithType:(UIButtonTypeCustom)];
            button.frame =  CGRectMake(itemX + tempI * (padding + itemW), itemY , itemW, itemH);
            
            [button setImage:[UIImage imageNamed:itemIcon] forState:UIControlStateNormal];
            [button setTitle:itemName forState:UIControlStateNormal];
            [button setTitleColor:UIColorFromRGB(0x666666) forState:UIControlStateNormal];
            button.titleLabel.font = iPhone5?[UIFont systemFontOfSize:10]:[UIFont systemFontOfSize:12];
            button.titleLabel.textAlignment = NSTextAlignmentCenter;
            [self.contentView addSubview:button];
            
            tempI ++;
            
            button.tag = i;
            

            
            [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            
        }

        
    }
    return self;
}

-(void)buttonClicked:(UIButton *)button{
    
    NSString *vc = _dataArr[button.tag][@"vc"];
    NSString *vcTitle = _dataArr[button.tag][@"itemName"];
    
    Class c = NSClassFromString(vc);
    UIViewController *viewController = [[c alloc]init];
    
    if (self.itemBlock) {
        self.itemBlock(viewController,vcTitle);
    }
}





@end
